2-1. Computation and Representation (1)

7/31/2025

https://introtcs.org/public/lec_02_representation.html

๊ฐœ์š”

The main takeaways from this chapter are:

  • We can represent all kinds of objects we want to use as inputs and outputs using binary strings. For example, we can use the binary basis to represent integers and rational numbers as binary strings (see Section 2.1.1 and Section 2.2).
  • We can compose the representations of simple objects to represent more complex objects. In this way, we can represent lists of integers or rational numbers, and use that to represent objects such as matrices, images, and graphs. Prefix-free encoding is one way to achieve such a composition (see Section 2.5.2).
  • A computational task specifies a map from an input to an outputโ€” a function. It is crucially important to distinguish between the โ€œwhatโ€ and the โ€œhowโ€, or the specification and implementation (see Section 2.6.1). A function simply defines which output corresponds to which input. It does not specify how to compute the output from the input, and as weโ€™ve seen in the context of multiplication, there can be more than one way to compute the same function.
  • While the set of all possible binary strings is infinite, it still cannot represent everything. In particular, there is no representation of the real numbers (with absolute accuracy) as binary strings. This result is also known as โ€œCantorโ€™s Theoremโ€ (see Section 2.4) and is typically referred to as the result that the โ€œreals are uncountable.โ€ It is also implied that there are different levels of infinity though we will not get into this topic in this book (see Remark 2.10).

The two โ€œbig ideasโ€ we discuss are Big Idea 1 - we can compose representations for simple objects to represent more complex objects and Big Idea 2 - it is crucial to distinguish between functionsโ€™ (โ€œwhatโ€) and programsโ€™ (โ€œhowโ€). The latter will be a theme we will come back to time and again in this book.

์ด๋ฒˆ ์žฅ์—์„œ๋Š” ์ด ๋„ค ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šด๋‹ค๊ณ  ํ•œ๋‹ค.

- '๋ชจ๋“  ์ข…๋ฅ˜'์˜ ์ž๋ฃŒ๋ฅผ binary strings("01110001110")๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์‚ฌ์‹ค.
- prefix-free encoding์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋ณต์žกํ•œ ์ž๋ฃŒ๋ฅผ ํฌํ˜„ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค. (๋ณต์žกํ•œ๊ฑธ ์ด์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋” ๋‹จ์ˆœํ•œ ๊ฒƒ๋“ค์˜ ๋ฌถ์Œ์œผ๋กœ ํ‘œํ˜„)
- ์ปดํ“จํ„ฐ๊ฐ€ ํ•˜๋Š”์ผ์€ ํ•จ์ˆ˜๋‹ค. ์ž…๋ ฅ -> ์ถœ๋ ฅ์„ ๋งตํ•‘ํ•˜๋Š” ์ผ. 
- ์ด์ง„๋ฒ•์˜ ์กฐํ•ฉ์€ ๋ฌดํ•œํ•˜์ง€๋งŒ, ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์„ธ์ƒ์˜ ๋ชจ๋“ ๊ฒƒ์„ ํ‘œํ˜„ํ•  ์ˆ˜๋Š” ์—†๋‹ค๋Š” ์‚ฌ์‹ค. (์นธํ† ์–ด์˜ ์ด๋ก )

์ด์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๊ธฐ

"ํ‘œํ˜„"

์šฐ๋ฆฌ๊ฐ€ ์ปดํ“จํ„ฐ์— ์‚ฌ์ง„, ์Œ์•…, ์˜์ƒ ๋“ฑ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์ง„์งœ๋กœ ์ €์žฅ๋˜๋Š” ๊ฒƒ์€ ๊ทธ๊ฒƒ๋“ค์˜ 'ํ‘œํ˜„'์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋‡Œ ๋งˆ์ €๋„ ๊ฐ๊ฐ๋œ ๊ทธ ์ž์ฒด๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š๊ณ  ๊ทธ๊ฒƒ์˜ ํ‘œํ˜„์„ ์ €์žฅํ•œ๋‹ค.
์ปดํ“จํ„ฐ๋Š” 0๊ณผ 1๋กœ ๋œ binary strings๋ฐ–์— ์ €์žฅํ•˜์ง€ ๋ชปํ•˜๋‹ˆ๊นŒ ๊ฒฐ๊ตญ ๋ชจ๋“ ๊ฑธ 0๊ณผ 1๋กœ ๋ฒˆ์—ญํ•˜๊ณ  ๊ทธ๊ฑธ ์ €์žฅํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ์ค‘์š”ํ•œ๊ฑด "์–ด๋–ป๊ฒŒ"์˜ ๋ฌธ์ œ๋‹ค. representation scheme.
์ž์—ฐ์ˆ˜, ์†Œ์ˆ˜, ๋ฌธ์ž์—ด, ์ด๋ฏธ์ง€, ์˜์ƒ ๋“ฑ๋“ฑ ๋ชจ๋“ ๊ฑธ ์–ด๋–ป๊ฒŒ 0๊ณผ1๋กœ ๋งŒ๋“ค๊ฒƒ์ธ๊ฐ€?

์ž์—ฐ์ˆ˜

์ž์—ฐ์ˆ˜๋ฅผ ์ด์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ์ต์ˆ™ํ•˜๋‹ค.
์ด๊ฑธ ํŒŒ์ด์ฌ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์ด๋ ‡๋‹ค.

def NtS(n):# natural numbers to strings
if n > 1:
return NtS(n // 2) + str(n % 2)
else:
return str(n % 2)
print(NtS(236))
# 11101100
print(NtS(19))
# 10011

โ€‹ํ•ด๋‹น ์ˆซ์ž๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ (0 or 1)์„ ๊ธฐ์ž….
๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ๊ทธ ์ˆซ์ž๋ฅผ 2๋กœ ๋‚˜๋ˆˆ๋’ค์— ๋ฐ˜๋ณต.
์–ธ์ œ๊นŒ์ง€? n์ด 2 ๋ฏธ๋งŒ์ผ ๋•Œ ๊นŒ์ง€.

์Œ์ˆ˜

์œ„์˜ NtS()๊ฒฐ๊ณผ ์•ž์— ์–‘์ˆ˜๋ผ๋ฉด 0์„ ์Œ์ˆ˜๋ผ๋ฉด 1์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
์ด๊ฑด ๊ณผ๊ฑฐ์˜ ๋ฐฉ๋ฒ•์ด๊ณ  ์š”์ฆ˜์˜ ๋ฐฉ๋ฒ•์€ ๋ฐ‘์—์„œ ์„œ์ˆ .

์–ด๋–ป๊ฒŒ ๊ตฌ๋ณ„ํ•˜๋‚˜?

์œ„์—์„œ๋„ ๋ณด๋ฉด NtS(19)์˜ ๊ฒฐ๊ณผ๋ฌผ์€ 10011์ธ๋ฐ, 19๋Š” ์–‘์ˆ˜๊ฐ€ ์•„๋‹Œ๊ฐ€?
๋งจ ์•ž์— ์žˆ๋Š”๊ฒŒ ์Œ์ˆ˜์™€ ์–‘์ˆ˜๋ฅผ ๊ตฌ๋ณ„ํ•˜๋Š” ์šฉ๋„๋กœ ์ถ”๊ฐ€ํ•œ ๋น„ํŠธ์ธ์ง€ ์•„๋‹Œ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ?
๊ทธ๋Ÿฌ๋ฉด ๋” ๊ทผ๋ณธ์ ์ธ ์งˆ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
10011010101 ์ด๋ ‡๊ฒŒ ์žˆ๋Š”๊ฒŒ ์• ์ดˆ์— ์ˆซ์ž์ธ๊ฑธ ์šฐ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ?
๊ฐ„๋‹จํžˆ ๋Œ€๋‹ตํ•˜์ž๋ฉด ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•ด๋‹น ์ž๋ฃŒ์˜ ์ž๋ฃŒํ˜•์„ ๋ฏธ๋ฆฌ ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์—.
๋˜‘๊ฐ™์€ 1110101์ด๋ผ๋„ ๊ทธ๊ฒŒ int์ธ์ง€ string์ธ์ง€ ์•„๋‹ˆ๋ฉด ํ˜น์€ ๋‹ค๋ฅธ ๋ฌด์—‡๊ฐ€์ธ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„๋œ๋‹ค๋Š”๊ฒƒ์ด๋‹ค.

Two's complement representation

์—ฌ๊ธฐ์„œ๋Š” ์ •ํ™•ํžˆ ์š”์ฆ˜์€ ์–ด๋–ป๊ฒŒ ์Œ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์•Œ๋ ค์ค€๋‹ค.
4๋น„ํŠธ ํ‘œํ˜„๋ฒ•์—์„œ -8์„ ๋‚˜ํƒ€๋‚ด๋ณด์ž.
$n=4-1 , ZtS_{3}(โˆ’8)=NtS_4(2^4โˆ’8)=1000$
๊ทธ๋Ÿผ 4๋น„ํŠธ์—์„œ 16์€ ์–ด๋–ป๊ฒŒ ๋‚˜ํƒ€๋‚ผ๊นŒ?
$NtS(16)=10000$ ์ธ๋ฐ ์—ฌ๊ธฐ์„œ ์•ž์˜ 4์ž๋ฆฌ๋งŒ ๊ฐ€์ ธ์˜ค๋‹ˆ๊นŒ 0000์ด๋‹ค.
๊ทธ๋ž˜์„œ 4๋น„ํŠธํ‘œํ˜„๋ฒ• ์•ˆ์—์„œ 1000์€ 16์ด ์•„๋‹ˆ๋ผ -8์ด ๋œ๋‹ค.
๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ๊ณผ๊ฑฐ์˜ sign bit ๋ฐฉ์‹๊ณผ ๊ฐ™๋‹ค. ์•ž์ด 1์ด๋ฉด ์Œ์ˆ˜ 0์ด๋ฉด ์–‘์ˆ˜.

์œ ๋ฆฌ์ˆ˜

-5/8 ์ด๋ผ๋Š” ์œ ๋ฆฌ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ• ๊นŒ.
-5 -> 1101, 8->01000 ํ•˜๋ฉด (1101,01000) ๋‘ ์ด์ง„์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค.
์ด๊ฑธ ๊ฐ ์ˆซ์ž๋ฅผ ๋‘๋ฒˆ์”ฉ ๋ฐ˜๋ณตํ•œ๋‹ค. 0์€ 00์œผ๋กœ 1์€ 11๋กœ.
11110011,0011000000 ์ด ๋˜๊ณ  ๊ทธ ๋‘˜ ์‚ฌ์ด์— 01์„ ๋„ฃ๊ณ  ์žˆ๋Š”๋‹ค.
11110011 01 0011000000(๊ตฌ๋ณ„ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ŠคํŽ˜์ด์Šค๋ฅผ ๋„ฃ์Œ) = -5/8

์‹ค์ˆ˜(๋ถ€๋™์†Œ์ˆ˜์  ํ‘œ๊ธฐ)

์–ด๋–ค ์‹ค์ˆ˜ x๊ฐ€ ์žˆ์œผ๋ฉด x์™€ ๊ทผ์ ‘ํ•œ $b * 2^e$ ๋ฅผ ์ฐพ์•„์„œ (b,e) ์Œ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.
12.75 ์€ ์ด๋ ‡๊ฒŒ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

  1. ์ด์ง„๋ฒ• ๋ณ€ํ™˜
    12๋Š” ์ด์ง„์ˆ˜๋กœ 1100
    $0.75 = 0.5 + 0.25 = 1/2 + 1/2^2 = 0.11$ ๋‘˜์„ ํ•ฉ์ณ์„œ 1100.11

  2. Scientific notation
    1100.11์ด๋ž€ ์ˆซ์ž๋ฅผ ์•ž์— ํ•œ ์ž๋ฆฌ๋งŒ ๋‚จ๊ธฐ๋„๋ก ์†Œ์ˆ˜์ ์„ ์˜ฎ๊ธฐ๋ฉด
    1.10011์ด ๋˜๊ณ  ์†Œ์ˆ˜์ ์€ 3์ž๋ฆฌ๋ฅผ ์˜ฎ๊ฒผ์œผ๋‹ˆ $1.10011 * 2^3$ ์ด๋ผ๊ณ  ํ‘œ๊ธฐ.

  3. ๊ฐ ๋ถ€๋ถ„ ๊ตฌํ•˜๊ธฐ
    ๋ถ€ํ˜ธ: 12.75๋Š” ์–‘์ˆ˜๋‹ˆ๊นŒ sign bit์€ 0.
    ์ง€์ˆ˜: 3 + 127 = 130 = 10000010
    ๊ฐ€์ˆ˜: 10011 (์œ„์˜ 2๋‹จ๊ณ„ ์ˆซ์ž์—์„œ ์†Œ์ˆ˜์  ๋’ค ๋ถ€๋ถ„๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์œผ๋กœ)
    12.75 = 0 100000010 10011000000000000000000

12.3 ์ฒ˜๋Ÿผ ๋”ฑ ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์‹ค์ˆ˜๋Š”?
12 -> 1100์€ ๋™์ผ.
0.3์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ•˜๋‚˜?
0.3 ๋ฅผ 2์”ฉ ๊ณฑํ•˜๋ฉด์„œ ์†Œ์ˆ˜์  ์•ž์— ์ˆซ์ž๋ฅผ ์ ๊ณ  ๋ฒ„๋ฆฐ๋‹ค.
0.3 * 2 = 0.6 -> 0
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
์ด๋ ‡๊ฒŒ ๋ฌดํ•œํžˆ ๊ฐ€๋‹ค๊ฐ€ ์ •ํ•ด์ง„ ์ž๋ฆฟ์ˆ˜์—์„œ ๋ฉˆ์ถ˜๋‹ค.(๋ณดํ†ต์€ 23๋น„ํŠธ. ์™œ๋ƒํ•˜๋ฉด float 32๋น„ํŠธ์—์„œ ๋ถ€ํ˜ธ 1๋น„ํŠธ, ์ง€์ˆ˜ 8๋น„ํŠธ ๋ฅผ ๋นผ๋ฉด 23๋น„ํŠธ๋งŒ ๋‚จ๊ธฐ ๋•Œ๋ฌธ์—)
๊ทธ๋ž˜์„œ 1100.01001.......0 ๊นŒ์ง€ ๊ตฌํ•œ๋‹ค์Œ์— ๊ทธ๊ฑธ
์ •๊ทœํ™”๋ฅผ ๊ฑฐ์ณ 1.10001001......0 * 2^3
๊ทธ๋ž˜์„œ 12.3 = 0 10000010 10001001100110011001101